package cs294s.liveradar;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import cs294s.liveradar.data.Blip;
import cs294s.liveradar.service.Feed;
import cs294s.liveradar.service.FeedManager;
import cs294s.liveradar.service.Util;

public class BlipAdapter extends BaseAdapter {

    /**
     * Maintains the state of our data
     */
    private FeedManager mBlipManager;
    private int feedPosition = 0;
    private Feed feed;

    private Context mContext;
    
    private MyDataSetObserver mObserver;

    /**
     * Used by the {@link ImageManager} to report changes in the list back to
     * this adapter.
     */
    private class MyDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }

        @Override
        public void onInvalidated() {
            notifyDataSetInvalidated();
        }
    }
    
    public BlipAdapter(Context c, int feedPosition) {
        mBlipManager = FeedManager.getInstance(c);
        mContext = c;
        mObserver = new MyDataSetObserver();
        this.feedPosition = feedPosition;
        feed = mBlipManager.get(feedPosition);
        mBlipManager.addObserver(mObserver);
    }

    /**
     * Returns the number of images to display
     * 
     * @see android.widget.Adapter#getCount()
     */
    public int getCount() {
        return feed.size();
    }

    /**
     * Returns the image at a specified position
     * 
     * @see android.widget.Adapter#getItem(int)
     */
    public Object getItem(int position) {
        return feed.get(position);
    }

    /**
     * Returns the id of an image at a specified position
     * 
     * @see android.widget.Adapter#getItemId(int)
     */
    public long getItemId(int position) {
        Blip b =feed.get(position);
        return b.getId();
    }

    /**
     * Returns a view to display the image at a specified position
     * 
     * @param position The position to display
     * @param convertView An existing view that we can reuse. May be null.
     * @param parent The parent view that will eventually hold the view we return.
     * @return A view to display the image at a specified position
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            // Make up a new view
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.blip_list_item, null);
        } else {
            // Use convertView if it is available
            view = convertView;
        }
        Blip b = feed.get(position);

        /*
        ImageView i = (ImageView) view.findViewById(R.id.blipListItem_image);
        i.setImageBitmap(b.getBitmap());
        */
        //i.setBackgroundResource(R.drawable.picture_frame);
        
        TextView t_d = (TextView) view.findViewById(R.id.blipListItem_description);
        t_d.setText(b.getDescription());
        TextView t_t = (TextView) view.findViewById(R.id.blipListItem_time);
        t_t.setText(Util.timeFormat(b.getEventTime()));
        TextView t_l = (TextView) view.findViewById(R.id.blipListItem_location);
        t_l.setText("Tags: " + b.tagsToString());
        
        
        return view;
    }
}
